<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *售后列表
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';

$tabs=array(
	array('op'=>'list','title'=>'用户反馈资料'),
	array('op'=>'edit','title'=>'添加反馈资料'),
);
$batch=array(
	//array('type'=>'post','url'=>'/member.list/setstore/','title'=>'一键归属门店'),
);
//售后列表
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=array();
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($this->session('classid')){
		$params['classid']=$this->session('classid');
		$return['params']['classid']=$this->session('classid');
	}
	if(!empty($_RQ['title'])){
		$params2=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
		$xueshengids=$this->getall('xuesheng',$params2,array('id','title'),'id');
		if($xueshengids){
			$xueshengids=array_keys($xueshengids);
		}else{
			$xueshengids=-1;
		}
		$params['xueshengid']=$xueshengids;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = $_RQ['psize']?:30;
	$list=pdo_getslice('jy_yxw_jiuye_log',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['member']=$this->model('xuesheng')->member_show($item['xueshengid']);
			$item['classid']=pdo_getcolumn('jy_yxw_xuesheng',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']),'classid');
			$item['sn']=pdo_getcolumn('jy_yxw_xuesheng',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']),'sn');
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	//页面显示设置
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'export'=>'export',
		'list'=>array('data'=>'list','del_message'=>'确认删除该反馈资料记录吗？','total'=>$total,'psize'=>$psize,'edit'=>false,'html_to'=>'list_html','list'=>array(
			array('name'=>'member','title'=>'用户信息','type'=>'member'),
			array('name'=>'sn','title'=>'编号','type'=>'string'),
			array('name'=>'classid','title'=>'售后群','type'=>'select','options'=>'class'),
			array('name'=>'remark','title'=>'反馈资料内容','type'=>'string'),
			array('name'=>'createtime','title'=>'日期','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','action'=>'jy_yxw/xuesheng.jiuyefile/lookimages','title'=>'查看反馈资料'),
			array('type'=>'href','action'=>'jy_yxw/xuesheng.jiuyefile/edit','title'=>'编辑反馈资料'),
		),'batch'=>$batch),
		'search'=>array(
			array('type'=>'string','width'=>'15','title'=>'售后名称','name'=>'title','help'=>''),
		),
	);
	$return['class']=$this->getall('class',array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');	
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_yxw_jiuye_log',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array();
	}
	$item['images']=json_decode($item['images'],true);
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回反馈资料列表'),
		),
		'edit'=>array(
			array('name'=>'xueshengid','title'=>'选择售后','type'=>'choose','action'=>'jy_yxw/get_xuesheng/'),	
			array('type'=>'images','title'=>'上传反馈资料','name'=>'images','help'=>''),
			array('name'=>'remark','title'=>'反馈资料内容','type'=>'string'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['xueshengid']){
		exi('请选择售后！','error');
	}
	if(!$postdata['images']){
		exi('请上传反馈资料！','error');
	}
	$data=array(
		'xueshengid'=>$postdata['xueshengid'],
		'remark'=>$postdata['remark'],
		'images'=>ijson_encode($postdata['images']),
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_yxw_jiuye_log',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		$data['storeid']=$_SESSION['storeid'];
		$data['classid']=$this->session('classid');
		$data['createtime']=time();
		pdo_insert('jy_yxw_jiuye_log',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='lookimages'){
	$id=$_RQ['id'];
	$item=$this->get('jiuye_log',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']));
	if(!$item){
		exi('指定记录不存在！','error');
	}
	$item['images']=json_decode($item['images'],true);
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'查看文件',
		'look'=>array(
			array('name'=>'images','title'=>'文件图片','type'=>'images'),
		),
	);
	exi($return);
	exit;
}
if($_RQ['op']=='export'){
	$filename='candel/'.random(15);
	@mkdir(ROOT_AT.'candel/');
	@mkdir(ROOT_AT.$filename);
	$readme='[温馨提示]：文件分两部分，一个是售后列表，一个是售后反馈表图片文件夹！';
	file_put_contents(ROOT_AT.$filename.'/readme.txt',$readme);
	//售后列表
	$return['params']=array();
	$params=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
	if($_RQ['storeid']){
		$params['storeid']=intval($_RQ['storeid']);
		$return['params']['storeid']=$_RQ['storeid'];
	}
	if($this->session('classid')){
		$params['classid']=$this->session('classid');
		$return['params']['classid']=$this->session('classid');
	}
	if(!empty($_RQ['title'])){
		$params2=array('storeid'=>$_SESSION['storeid'],'uniacid'=>$_SESSION['uniacid']);
		$params2['title like']='%'.$_RQ['title'].'%';
		$return['params']['title']=$_RQ['title'];
		$xueshengids=$this->getall('xuesheng',$params2,array('id','title'),'id');
		if($xueshengids){
			$xueshengids=array_keys($xueshengids);
		}else{
			$xueshengids=-1;
		}
		$params['xueshengid']=$xueshengids;
	}
	$list=pdo_getall('jy_yxw_jiuye_log',$params,$fields);
	if($list){
		foreach($list as &$item){
			$xuesheng=pdo_get('jy_yxw_xuesheng',array('id'=>$item['xueshengid'],'uniacid'=>$_SESSION['uniacid']));
			$item['title']=$xuesheng['title'];
			$item['mobile']=$xuesheng['mobile'];
			$item['mobile2']=$xuesheng['mobile2'];
			$item['status']=$this->xuesheng_status[$xuesheng['status']];
			$item['createtime']=date('Y/m/d H:i:s',$xuesheng['createtime']);
			$images=json_decode($item['images'],true);
			$this->model('xuesheng')->downimages($images,$filename,$item['title'],'反馈表');
		}
	}
	$header=array(
		'id'=>'售后ID',
		'title'=>'售后姓名',
		'mobile'=>'手机号',
		'mobile2'=>'手机号',
		'status'=>'售后状态',
		'createtime'=>'注册时间',
	);
	model('core/tools')->export2excel($header,$list,'售后列表',true,$filename);
	$baoname='售后反馈表'.random(3);
	cfc('tools')->down_zip(ROOT_AT.$filename,$baoname,true);
}
if($_RQ['op']=='del'){
	exi('不能删除！','error');	
}